package com.rzico.sbl.ui.manager

import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.CupboardSummary
import com.rzico.sbl.model.SummaryBean
import com.rzico.sbl.viewmodel.PickupViewModel
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.extend.find
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.extend.gone
import com.xinnuo.common.extend.inflate
import com.xinnuo.common.extend.notEmpty
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class PickupFillSummaryActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    private var mPickupId = ""

    override fun getViewModel() = get<PickupViewModel>()

    override fun initData() {
        initTitle(title = "补货统计")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData()
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关缺货信息！"
                }

                recycleList.also { view ->
                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) }
                    )

                    view.addItemDecoration(SpaceTBDecoration())

                    mAdapter = SlimAdapter.creator()
                        .register<SummaryBean>(R.layout.item_pickup_summary) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_summary_name, bean.cabinetName)
                                text(R.id.item_summary_no, bean.cabinetNo)
                                text(R.id.item_summary_count, bean.count)
                                text(
                                    R.id.item_summary_type,
                                    when (bean.type) {
                                        "0" -> "云柜-普通版"
                                        "1" -> "云柜-触屏版"
                                        "2" -> "云柜-普通扩展版"
                                        "3" -> "云柜-触屏扩展版"
                                        else -> ""
                                    }
                                )

                                getView<View>(R.id.item_summary_detail).isGone =
                                    bean.items.none { it.num.intValue > 0 }

                                with<LinearLayout>(R.id.item_summary_content) { layout ->
                                    layout.removeAllViews()

                                    bean.items.forEach {
                                        if (it.num.intValue > 0) {
                                            layout.addView(
                                                inflate<View>(R.layout.content_pickup_summary_hint)
                                                    .apply {
                                                        find<TextView>(R.id.conent_title).text = it.productName
                                                        find<TextView>(R.id.conent_num).text = it.num
                                                    }
                                            )
                                        }
                                    }
                                }

                                clicked {
                                    startActivityEx<PickupCupboardCodeActivity>(
                                        "pickId" to mPickupId,
                                        "cupboardId" to bean.cabinetId
                                    )
                                }
                            }
                        }
                        .attachTo(view)
                }
            }
    }

    override fun initListener() {
        mPickupId = intent.getExtra("pickId")
    }

    /**
     * 补货统计列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .cupboardSummary(
                keyWord = "",
                shopId = mPickupId,
                onSuccess = {
                    val responeList = arrayListOf<CupboardSummary>()
                    responeList.apply {
                        clear()
                        addItems(it)
                    }

                    responeList
                        .groupBy { it.cabinetId }
                        .mapTo(mList) {
                            SummaryBean(
                                cabinetId = it.value[0].cabinetId,
                                cabinetNo = it.value[0].cabinetNo,
                                cabinetName = it.value[0].cabinetName,
                                status = it.value[0].status,
                                type = it.value[0].type,
                                count = it.value[0].count,
                                items = it.value
                            )
                        }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            clear()
            mAdapter.notifyDataSetChanged()
        }

        getData()
    }

}